package com.propertypoint.presentation.actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.propertypoint.persitence.dao.impl.AdminDAO;
import com.propertypoint.persitence.dao.impl.AdminDAOImpl;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String PENDING = "PENDING";
	private static String APPROVED = "APPROVED";
       

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String checked = request.getParameter("checked");
		PrintWriter out = response.getWriter();
	
		//TODO: update database accordingly
		String status;
		if(checked.equals("true")){
			status = APPROVED;
		}else{
			status = PENDING;
		}
		AdminDAO dao = new AdminDAOImpl();
		dao.updateAdsStatus(id, status);
		//System.out.printf("%d %s %s\n", id,status, checked);
		out.printf("Ad %s changed to: %s", id, status);

		//System.out.printf("%s %s\n", id, checked);
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
